package servlet;

import pojo.User;
import service.Userservice;
import service.impl.UserServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/add")
public class AddUserServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //请求编码设置
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("name");
        String pass= req.getParameter("pass");

        Userservice userservice = new UserServiceimpl();
       int result = userservice.addUser(new User(name,pass));
       if(result>0){
           resp.sendRedirect("user");
       }
     else {
         resp.sendRedirect("addUser.jsp");
       }
    }
}